import { createContext, useContext } from 'react'
import { toJS } from 'mobx'
import { observer } from 'mobx-react-lite'
import Request from './modules/request'

// store
const store = {
  request: new Request(),
}

// 创建context：把 store 放进 context 中
const StoreContext = createContext(store)

// 配置 hooks
const useStore = () => {
  const stores = useContext(StoreContext)
  if (!stores) {
    throw new Error('no store')
  }
  return stores
}

export { StoreContext, store } // 最外层组件使用
export { useStore, observer, toJS } // 导出 observer、toJS 为了用到时少引入
